package cqie.frame.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cqie.frame.entity.EvaluateTarget;
import cqie.frame.entity.EvaluateTargetOption;
import cqie.frame.entity.EvaluateType;
import cqie.frame.entity.po.EvaluateTargetPo;
import cqie.frame.entity.po.SelectListItem;
import cqie.frame.entity.vo.EvaluateTargetTreeData;
import cqie.frame.entity.vo.EvaluateTargetVo;
import cqie.frame.response.CodeMsg;
import cqie.frame.response.ResultJson;
import cqie.frame.service.IEvaluateTargetOptionService;
import cqie.frame.service.IEvaluateTargetService;
import cqie.frame.service.IEvaluateTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 评价指标(EvaluateTarget)表控制层
 *
 * @author makejava
 * @since 2021-10-08 10:40:42
 */
@Api
@RestController
@RequestMapping("/evaluateTarget")
public class EvaluateTargetController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private IEvaluateTargetService iEvaluateTargetService;
    @Resource
    private IEvaluateTargetOptionService iEvaluateTargetOptionService;
    @Resource
    private IEvaluateTypeService iEvaluateTypeService;

    @ApiOperation(value = "新增提交", response = EvaluateTargetVo.class)
    @PostMapping(value = "/add")
    public ResultJson add(@RequestBody EvaluateTargetPo po) {
        try {
            //1.提交主表
            EvaluateTarget mo = new EvaluateTarget();
            BeanUtils.copyProperties(po, mo);
            iEvaluateTargetService.save(mo);
            //2.提交子表
            if (po.getEvaluateTargetOptionList() != null && po.getEvaluateTargetOptionList().size() > 0) {
                for (EvaluateTargetOption item : po.getEvaluateTargetOptionList()) {
                    item.setEvaluateTargetId(mo.getId()); //设置主表Id
                }
                iEvaluateTargetOptionService.saveBatch(po.getEvaluateTargetOptionList());
            }
            //3.构造返回值(mo转vo)
            EvaluateTargetVo vo = mappingMoToPo(mo, true);
            return ResultJson.success(vo);
        } catch (Exception ex) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    @ApiOperation(value = "删除")
    @GetMapping(value = "/remove")
    public ResultJson remove(Long id) {
        try {
            //删除子表
            QueryWrapper<EvaluateTargetOption> qwDetail = new QueryWrapper<>();
            qwDetail.eq("evaluateTargetId", id);
            iEvaluateTargetOptionService.remove(qwDetail);
            //删除主表
            iEvaluateTargetService.removeById(id);
            return ResultJson.success("删除成功");
        } catch (Exception ex) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    //删除选中评价指标，多选
    @ApiOperation(value = "删除选中", notes = "删除选中。")
    @PostMapping(value = "/removeChecked")
    public ResultJson removeChecked(@RequestBody Long[] evaluateTargetIdList) {
        try {
            //先删除指标备选表
            for (int i = 0; i < evaluateTargetIdList.length; i++) {
                //删除子表
                QueryWrapper<EvaluateTargetOption> qwDetail = new QueryWrapper<>();
                qwDetail.eq("evaluateTargetId", evaluateTargetIdList[i]);
                iEvaluateTargetOptionService.remove(qwDetail);
            }
            for (int i = 0; i < evaluateTargetIdList.length; i++) {
                iEvaluateTargetService.removeById(evaluateTargetIdList[i]);
            }
            return ResultJson.success("删除成功");
        } catch (Exception e) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    @ApiOperation(value = "修改提交", response = EvaluateTargetVo.class)
    @PostMapping(value = "/edit")
    public ResultJson edit(@RequestBody EvaluateTargetPo po) {
        try {
            //1.更新主表
            EvaluateTarget mo = new EvaluateTarget();
            BeanUtils.copyProperties(po, mo);
            iEvaluateTargetService.updateById(mo);
            //2.更新从表
            //方案1：先删除，再追加
            //方案2：根据从表数据分别执行新增、修改、删除命令
            //因子表有自增Id,在此采用方案2为宜
            //2.1确定从表需删除的id范围
            QueryWrapper<EvaluateTargetOption> qwDetail = new QueryWrapper<>();
            qwDetail.eq("evaluateTargetId", mo.getId());
            List<EvaluateTargetOption> crudeDetails = iEvaluateTargetOptionService.list(qwDetail);
            List<Long> delIdRange = crudeDetails.stream().map(EvaluateTargetOption::getId).collect(Collectors.toList());//默认全部删除
            for (EvaluateTargetOption detail : po.getEvaluateTargetOptionList()) {
                if (detail.getId() != null) {
                    delIdRange.remove(detail.getId());//提交数据有Id，以修改方式更新，不删除，因此从删除范围移出
                }
            }
            //2.2新增或修改方式更新从表
            if (po.getEvaluateTargetOptionList().size() > 0) {
                iEvaluateTargetOptionService.saveOrUpdateBatch(po.getEvaluateTargetOptionList());
            }
            //2.3删除从表
            if (delIdRange.size() > 0) {
                QueryWrapper<EvaluateTargetOption> qwDelete = new QueryWrapper<>();
                qwDelete.in("id", delIdRange);
                iEvaluateTargetOptionService.remove(qwDelete);
            }
            //3.构造返回值(mo转vo)
            EvaluateTargetVo vo = mappingMoToPo(mo, true);

            return ResultJson.success(vo);
        } catch (Exception ex) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    @ApiOperation(value = "查看详情", notes = "", response = EvaluateTargetVo.class)
    @GetMapping(value = "/details")
    public ResultJson details(Long id) {
        try {
            //1.获取mo
            EvaluateTarget mo = iEvaluateTargetService.getById(id);
            if (mo == null) {
                return ResultJson.error(new CodeMsg(100, "指定主键对应实体不存在"));
            }
            //2.映射成vo
            EvaluateTargetVo vo = mappingMoToPo(mo, true);

            return ResultJson.success(vo);
        } catch (Exception ex) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    @ApiOperation(value = "评价指标树", notes = "构造elementUi需要的树表数据", response = EvaluateTargetTreeData.class)
    @PostMapping(value = "/listTreeData")
    public ResultJson listTreeData() {
        try {
            List<EvaluateTargetTreeData> ret = new ArrayList<>();
            //全部指标
            List<EvaluateTarget> allEvaluateTarget = iEvaluateTargetService.list(null);
            //全部备选项
            List<EvaluateTargetOption> allEvaluateTargetOption = iEvaluateTargetOptionService.list(null);
            //全部评价类型
            List<EvaluateType> allEvaluateType = iEvaluateTypeService.list(null);

            List<EvaluateTarget> first = allEvaluateTarget.stream().filter(x -> x.getParentId() == null).sorted(Comparator.comparing(EvaluateTarget::getSortCode)).collect(Collectors.toList());
            for (EvaluateTarget mo : first) {
                ret.add(generateTreeData(mo, allEvaluateTarget, allEvaluateTargetOption, allEvaluateType));
            }
            return ResultJson.success(ret);
        } catch (Exception ex) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    /**
     * 递归方法--EvaluateTarget-->EvaluateTargetTreeData
     * 要映射评价类型+从表备选项列表
     * 构造树表数据用
     */
    private EvaluateTargetTreeData generateTreeData(EvaluateTarget mo, List<EvaluateTarget> allEvaluateTarget, List<EvaluateTargetOption> allEvaluateTargetOption, List<EvaluateType> allEvaluateType) {
        EvaluateTargetTreeData node = new EvaluateTargetTreeData();
        BeanUtils.copyProperties(mo, node);
        // 映射--上级指标名称
        if (mo.getParentId() != null) {
            EvaluateTarget parentEvaluateTarget = allEvaluateTarget.stream().filter(x -> x.getId().equals(mo.getParentId())).findFirst().orElse(null);
            if (parentEvaluateTarget != null) {
                node.setParentName(parentEvaluateTarget.getName());
            }
        }
        //映射--评价类型名称
        if (mo.getEvaluateTypeId() != null) {
            EvaluateType evaluateType = allEvaluateType.stream().filter(x -> x.getId().equals(mo.getEvaluateTypeId())).findFirst().orElse(null);
            if (evaluateType != null) {
                node.setEvaluateTypeName(evaluateType.getName());
            }
        }
        //映射--备选项列表
        List<EvaluateTargetOption> details = allEvaluateTargetOption.stream().filter(x -> x.getEvaluateTargetId().equals(mo.getId())).collect(Collectors.toList());
        node.setEvaluateTargetOptionList(details);
        //映射--下级指标
        List<EvaluateTarget> children = allEvaluateTarget.stream()
                .filter(x -> x.getParentId() != null && x.getParentId().equals(mo.getId()))
                .sorted(Comparator.comparing(EvaluateTarget::getSortCode))
                .collect(Collectors.toList());
        List<EvaluateTargetTreeData> subNodeList = new ArrayList<>();
        for (EvaluateTarget child : children) {
            subNodeList.add(generateTreeData(child, allEvaluateTarget, allEvaluateTargetOption, allEvaluateType));
        }
        node.setChildren(subNodeList);
        return node;
    }

    @ApiOperation(value = "上级指标备选项", notes = "elementUi需要的树表数据", response = EvaluateTargetTreeData.class)
    @GetMapping(value = "/listParentData")
    public ResultJson listParentData(Long id) {
        try {
            List<EvaluateTargetTreeData> ret = new ArrayList<>();
            //限制不跨类型选上级指标(更完善的方案是评价类型确定后实时更新上级备选项)
            QueryWrapper<EvaluateTarget> qw = new QueryWrapper<>();
            if (id != null) {
                qw.inSql("evaluateTypeId", "select evaluateTypeId from evaluate_target where id=" + id);
            }
            List<EvaluateTarget> all = iEvaluateTargetService.list(qw);
            List<Long> excludeIdList = new ArrayList<>();
            if (id != null) {
                //排除自己及全部后代
                addSelfAndChildren(id, excludeIdList, all);
                all = all.stream().filter(x -> excludeIdList.contains(x.getId()) == false).collect(Collectors.toList());
            }
            List<EvaluateTarget> first = all.stream()
                    .filter(x -> x.getParentId() == null)
                    .sorted(Comparator.comparing(EvaluateTarget::getSortCode))
                    .collect(Collectors.toList());
            for (EvaluateTarget mo : first) {
                ret.add(generateTreeData(mo, all));
            }
            return ResultJson.success(ret);
        } catch (Exception ex) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    /**
     * 递归方法--添加自己及子女到idList
     * 构造上级指标备选项用
     */
    private void addSelfAndChildren(Long selfId, List<Long> idList, List<EvaluateTarget> all) {
        //添加自己的Id
        idList.add(selfId);
        //查找子女
        List<EvaluateTarget> children = all.stream()
                .filter(x -> x.getParentId() != null && x.getParentId().equals(selfId))
                .collect(Collectors.toList());
        //递归添加子女的Id
        for (EvaluateTarget child : children) {
            addSelfAndChildren(child.getId(), idList, all);
        }
    }

    /**
     * 递归方法--EvaluateTarget-->EvaluateTargetTreeData
     * 不映射评价类型+从表备选项列表
     * 构造上级指标备选项用
     */
    private EvaluateTargetTreeData generateTreeData(EvaluateTarget mo, List<EvaluateTarget> allEvaluateTarget) {
        EvaluateTargetTreeData node = new EvaluateTargetTreeData();
        BeanUtils.copyProperties(mo, node);
        // 映射--上级指标名称
        if (mo.getParentId() != null) {
            EvaluateTarget parentEvaluateTarget = allEvaluateTarget.stream().filter(x -> x.getId().equals(mo.getParentId())).findFirst().orElse(null);
            if (parentEvaluateTarget != null) {
                node.setParentName(parentEvaluateTarget.getName());
            }
        }
        //映射--下级指标
        List<EvaluateTarget> children = allEvaluateTarget.stream()
                .filter(x -> x.getParentId() != null && x.getParentId().equals(mo.getId()))
                .sorted(Comparator.comparing(EvaluateTarget::getSortCode))
                .collect(Collectors.toList());
        List<EvaluateTargetTreeData> subNodeList = new ArrayList<>();
        for (EvaluateTarget child : children) {
            subNodeList.add(generateTreeData(child, allEvaluateTarget));
        }
        node.setChildren(subNodeList);
        return node;
    }

    /**
     * 映射mo为vo
     */
    private EvaluateTargetVo mappingMoToPo(EvaluateTarget mo, Boolean isContainDetails) {
        EvaluateTargetVo vo = new EvaluateTargetVo();
        try {
            BeanUtils.copyProperties(mo, vo);
            //映射--上级指标名称
            if (mo.getParentId() != null) {
                EvaluateTarget parentEvaluateTarget = iEvaluateTargetService.getById(mo.getParentId());
                if (parentEvaluateTarget != null) {
                    vo.setParentName(parentEvaluateTarget.getName());
                }
            }
            //映射--评价类型名称
            if (mo.getEvaluateTypeId() != null) {
                EvaluateType evaluateType = iEvaluateTypeService.getById(mo.getEvaluateTypeId());
                if (evaluateType != null) {
                    vo.setEvaluateTypeName(evaluateType.getName());
                }
            }
            //映射--备选项列表
            if (isContainDetails) { //需要子表的明细
                QueryWrapper<EvaluateTargetOption> qw = new QueryWrapper<>();
                qw.eq("evaluateTargetId", mo.getId());
                vo.setEvaluateTargetOptionList(iEvaluateTargetOptionService.list(qw));
            }
        } catch (Exception ex) {

        }
        return vo;
    }

    @ApiOperation(value = "评价类型备选项", response = SelectListItem.class)
    @GetMapping(value = "/listAllEvaluateType")
    public ResultJson listAllEvaluateType() {
        try {
            List<SelectListItem> ret = new ArrayList<>();
            List<EvaluateType> roleList = iEvaluateTypeService.list(null);
            roleList.forEach(x -> {
                ret.add(SelectListItem.SelectListItem(x.getId(),
                        x.getName()));
            });
            return ResultJson.success(ret);
        } catch (Exception ex) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    @ApiOperation(value = "批量导入指标", notes = "导入Excel批量添加")
    @PostMapping(value = "/importExcel")
    public ResultJson importExcel(@RequestParam("file") MultipartFile file) {
        try {
            InputStream is = file.getInputStream();
            POIFSFileSystem fs = new POIFSFileSystem(is);
            HSSFWorkbook wb = new HSSFWorkbook(fs);
            Long typeId;
            int rowIndex;
            //1.学生评教
            typeId = 1L;
            HSSFSheet sheet = wb.getSheet("学生评教");
            List<EvaluateTarget> targetList = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                EvaluateTarget target = new EvaluateTarget();
                target.setEvaluateTypeId(typeId);
                rowIndex = 4 * i + 1;
                HSSFRow row = sheet.getRow(rowIndex);
                target.setName(row.getCell(0).getRichStringCellValue().getString().trim());
                target.setWeight(new BigDecimal(row.getCell(1).getNumericCellValue()));
                target.setSortCode((i + 1) * 10);
                targetList.add(target);
            }
            iEvaluateTargetService.saveBatch(targetList);
            //备选项
            List<EvaluateTargetOption> optionList = new ArrayList<>();
            for (EvaluateTarget target : targetList) {
                int index = targetList.indexOf(target);
                for (int j = 0; j < 4; j++) {
                    EvaluateTargetOption option = new EvaluateTargetOption();
                    option.setEvaluateTargetId(target.getId());
                    rowIndex = 4 * index + j + 1;
                    HSSFRow row = sheet.getRow(rowIndex);
                    option.setContent(row.getCell(2).getRichStringCellValue().getString().trim());
                    option.setScore(new BigDecimal(row.getCell(3).getNumericCellValue()));
                    option.setSortCode((j + 1) * 10);
                    optionList.add(option);
                }
            }
            //2.教师自评
            typeId = 2L;
            sheet = wb.getSheet("教师自评");
            targetList = new ArrayList<>();
            for (int i = 0; i < 9; i++) {
                EvaluateTarget target = new EvaluateTarget();
                target.setEvaluateTypeId(typeId);
                rowIndex = 4 * i + 1;
                HSSFRow row = sheet.getRow(rowIndex);
                target.setName(row.getCell(0).getRichStringCellValue().getString().trim());
                target.setWeight(new BigDecimal(row.getCell(1).getNumericCellValue()));
                target.setSortCode((i + 1) * 10);
                targetList.add(target);
            }
            iEvaluateTargetService.saveBatch(targetList);
            //备选项
            for (EvaluateTarget target : targetList) {
                int index = targetList.indexOf(target);
                for (int j = 0; j < 4; j++) {
                    EvaluateTargetOption option = new EvaluateTargetOption();
                    option.setEvaluateTargetId(target.getId());
                    rowIndex = 4 * index + j + 1;
                    HSSFRow row = sheet.getRow(rowIndex);
                    option.setContent(row.getCell(2).getRichStringCellValue().getString().trim());
                    option.setScore(new BigDecimal(row.getCell(3).getNumericCellValue()));
                    option.setSortCode((j + 1) * 10);
                    optionList.add(option);
                }
            }
            //3.同行评教
            typeId = 3L;
            sheet = wb.getSheet("同行评教");
            targetList = new ArrayList<>();
            for (int i = 0; i < 9; i++) {
                EvaluateTarget target = new EvaluateTarget();
                target.setEvaluateTypeId(typeId);
                rowIndex = 4 * i + 1;
                HSSFRow row = sheet.getRow(rowIndex);
                target.setName(row.getCell(0).getRichStringCellValue().getString().trim());
                target.setWeight(new BigDecimal(row.getCell(1).getNumericCellValue()));
                target.setSortCode((i + 1) * 10);
                targetList.add(target);
            }
            iEvaluateTargetService.saveBatch(targetList);
            //备选项
            for (EvaluateTarget target : targetList) {
                int index = targetList.indexOf(target);
                for (int j = 0; j < 4; j++) {
                    EvaluateTargetOption option = new EvaluateTargetOption();
                    option.setEvaluateTargetId(target.getId());
                    rowIndex = 4 * index + j + 1;
                    HSSFRow row = sheet.getRow(rowIndex);
                    option.setContent(row.getCell(2).getRichStringCellValue().getString().trim());
                    option.setScore(new BigDecimal(row.getCell(3).getNumericCellValue()));
                    option.setSortCode((j + 1) * 10);
                    optionList.add(option);
                }
            }
            //4.教研室主任评教
            typeId = 4L;
            sheet = wb.getSheet("教研室主任评教");
            rowIndex = 1;
            HSSFRow row = sheet.getRow(rowIndex);
            EvaluateTarget firstTarget = new EvaluateTarget();
            firstTarget.setEvaluateTypeId(typeId);
            firstTarget.setName(row.getCell(0).getRichStringCellValue().getString().trim());
            firstTarget.setWeight(new BigDecimal(row.getCell(1).getNumericCellValue()));
            firstTarget.setSortCode(10);
            iEvaluateTargetService.save(firstTarget);
            targetList = new ArrayList<>();
            for (int i = 0; i < 9; i++) {
                EvaluateTarget secondTarget = new EvaluateTarget();
                secondTarget.setEvaluateTypeId(typeId);
                secondTarget.setParentId(firstTarget.getId());
                rowIndex = 4 * i + 1;
                row = sheet.getRow(rowIndex);
                secondTarget.setName(row.getCell(2).getRichStringCellValue().getString().trim());
                secondTarget.setWeight(new BigDecimal(row.getCell(3).getNumericCellValue()));
                secondTarget.setSortCode((i + 1) * 10);
                targetList.add(secondTarget);
            }
            iEvaluateTargetService.saveBatch(targetList);
            //备选项
            for (EvaluateTarget target : targetList) {
                int index = targetList.indexOf(target);
                for (int j = 0; j < 4; j++) {
                    EvaluateTargetOption option = new EvaluateTargetOption();
                    option.setEvaluateTargetId(target.getId());
                    rowIndex = 4 * index + j + 1;
                    row = sheet.getRow(rowIndex);
                    option.setContent(row.getCell(4).getRichStringCellValue().getString().trim());
                    option.setScore(new BigDecimal(row.getCell(5).getNumericCellValue()));
                    option.setSortCode((j + 1) * 10);
                    optionList.add(option);
                }
            }
            targetList = new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                EvaluateTarget target = new EvaluateTarget();
                target.setEvaluateTypeId(typeId);
                rowIndex = 4 * i + 37;
                row = sheet.getRow(rowIndex);
                target.setName(row.getCell(0).getRichStringCellValue().getString().trim());
                target.setWeight(new BigDecimal(row.getCell(1).getNumericCellValue()));
                target.setSortCode((i + 2) * 10);
                targetList.add(target);
            }
            iEvaluateTargetService.saveBatch(targetList);
            //备选项
            for (EvaluateTarget target : targetList) {
                int index = targetList.indexOf(target);
                for (int j = 0; j < 4; j++) {
                    EvaluateTargetOption option = new EvaluateTargetOption();
                    option.setEvaluateTargetId(target.getId());
                    rowIndex = 4 * index + j + 37;
                    row = sheet.getRow(rowIndex);
                    option.setContent(row.getCell(4).getRichStringCellValue().getString().trim());
                    option.setScore(new BigDecimal(row.getCell(5).getNumericCellValue()));
                    option.setSortCode((j + 1) * 10);
                    optionList.add(option);
                }
            }
            //5.系主任评教
            typeId = 5L;
            sheet = wb.getSheet("系主任评教");
            //量考核
            rowIndex = 1;
            row = sheet.getRow(rowIndex);
            firstTarget = new EvaluateTarget();
            firstTarget.setEvaluateTypeId(typeId);
            firstTarget.setName(row.getCell(0).getRichStringCellValue().getString().trim());
            firstTarget.setWeight(new BigDecimal(row.getCell(1).getNumericCellValue()));
            firstTarget.setSortCode(10);
            iEvaluateTargetService.save(firstTarget);
            targetList = new ArrayList<>();
            for (int i = 0; i < 3; i++) {
                EvaluateTarget secondTarget = new EvaluateTarget();
                secondTarget.setEvaluateTypeId(typeId);
                secondTarget.setParentId(firstTarget.getId());
                rowIndex = 4 * i + 1;
                row = sheet.getRow(rowIndex);
                secondTarget.setName(row.getCell(2).getRichStringCellValue().getString().trim());
                secondTarget.setWeight(new BigDecimal(row.getCell(3).getNumericCellValue()));
                secondTarget.setSortCode((i + 1) * 10);
                targetList.add(secondTarget);
            }
            iEvaluateTargetService.saveBatch(targetList);
            //备选项
            for (EvaluateTarget target : targetList) {
                int index = targetList.indexOf(target);
                for (int j = 0; j < 4; j++) {
                    EvaluateTargetOption option = new EvaluateTargetOption();
                    option.setEvaluateTargetId(target.getId());
                    rowIndex = 4 * index + j + 1;
                    row = sheet.getRow(rowIndex);
                    option.setContent(row.getCell(8).getRichStringCellValue().getString().trim());
                    option.setScore(new BigDecimal(row.getCell(9).getNumericCellValue()));
                    option.setSortCode((j + 1) * 10);
                    optionList.add(option);
                }
            }
            //质考核
            rowIndex = 13;
            row = sheet.getRow(rowIndex);
            firstTarget = new EvaluateTarget();
            firstTarget.setEvaluateTypeId(typeId);
            firstTarget.setName(row.getCell(0).getRichStringCellValue().getString().trim());
            firstTarget.setWeight(new BigDecimal(row.getCell(1).getNumericCellValue()));
            firstTarget.setSortCode(20);
            iEvaluateTargetService.save(firstTarget);
            //工作态度
            EvaluateTarget secondTarget = new EvaluateTarget();
            secondTarget.setEvaluateTypeId(typeId);
            secondTarget.setParentId(firstTarget.getId());
            secondTarget.setName(sheet.getRow(rowIndex).getCell(2).getRichStringCellValue().getString().trim());
            secondTarget.setWeight(new BigDecimal(sheet.getRow(rowIndex).getCell(3).getNumericCellValue()));
            secondTarget.setSortCode(10);
            iEvaluateTargetService.save(secondTarget);
            //接受任务态度
            EvaluateTarget threeTarget = new EvaluateTarget();
            threeTarget.setEvaluateTypeId(typeId);
            threeTarget.setParentId(secondTarget.getId());
            threeTarget.setName(sheet.getRow(rowIndex).getCell(4).getRichStringCellValue().getString().trim());
            threeTarget.setWeight(new BigDecimal(sheet.getRow(rowIndex).getCell(5).getNumericCellValue()));
            threeTarget.setSortCode(10);
            iEvaluateTargetService.save(threeTarget);
            //备选项
            for (int j = 0; j < 4; j++) {
                rowIndex = 13 + j;
                EvaluateTargetOption option = new EvaluateTargetOption();
                option.setEvaluateTargetId(threeTarget.getId());
                option.setContent(sheet.getRow(rowIndex).getCell(8).getRichStringCellValue().getString().trim());
                option.setScore(new BigDecimal(sheet.getRow(rowIndex).getCell(9).getNumericCellValue()));
                option.setSortCode((j + 1) * 10);
                optionList.add(option);
            }
            //教学常规
            rowIndex = 17;
            threeTarget = new EvaluateTarget();
            threeTarget.setEvaluateTypeId(typeId);
            threeTarget.setParentId(secondTarget.getId());
            threeTarget.setName(sheet.getRow(rowIndex).getCell(4).getRichStringCellValue().getString().trim());
            threeTarget.setWeight(new BigDecimal(sheet.getRow(rowIndex).getCell(5).getNumericCellValue()));
            threeTarget.setSortCode(20);
            iEvaluateTargetService.save(threeTarget);
            targetList = new ArrayList<>();
            for (int i = 0; i < 7; i++) {
                rowIndex = 4 * i + 17;
                EvaluateTarget fourTarget = new EvaluateTarget();
                fourTarget.setEvaluateTypeId(typeId);
                fourTarget.setParentId(threeTarget.getId());
                fourTarget.setName(sheet.getRow(rowIndex).getCell(6).getRichStringCellValue().getString().trim());
                fourTarget.setWeight(new BigDecimal(sheet.getRow(rowIndex).getCell(7).getNumericCellValue()));
                fourTarget.setSortCode(10 * (i + 1));
                targetList.add(fourTarget);
            }
            iEvaluateTargetService.saveBatch(targetList);
            //备选项
            for (EvaluateTarget target : targetList) {
                int index = targetList.indexOf(target);
                for (int j = 0; j < 4; j++) {
                    EvaluateTargetOption option = new EvaluateTargetOption();
                    option.setEvaluateTargetId(target.getId());
                    rowIndex = 18 + 4 * index + j;
                    row = sheet.getRow(rowIndex);
                    option.setContent(row.getCell(8).getRichStringCellValue().getString().trim());
                    option.setScore(new BigDecimal(row.getCell(9).getNumericCellValue()));
                    option.setSortCode((j + 1) * 10);
                    optionList.add(option);
                }
            }
            //学术与研究水平
            rowIndex = 45;
            secondTarget = new EvaluateTarget();
            secondTarget.setEvaluateTypeId(typeId);
            secondTarget.setParentId(firstTarget.getId());
            secondTarget.setName(sheet.getRow(rowIndex).getCell(2).getRichStringCellValue().getString().trim());
            secondTarget.setWeight(new BigDecimal(sheet.getRow(rowIndex).getCell(3).getNumericCellValue()));
            secondTarget.setSortCode(20);
            iEvaluateTargetService.save(secondTarget);
            targetList = new ArrayList<>();
            for (int i = 0; i < 3; i++) {
                rowIndex = 45 + 4 * i;
                threeTarget = new EvaluateTarget();
                threeTarget.setEvaluateTypeId(typeId);
                threeTarget.setParentId(secondTarget.getId());
                threeTarget.setName(sheet.getRow(rowIndex).getCell(4).getRichStringCellValue().getString().trim());
                threeTarget.setWeight(new BigDecimal(sheet.getRow(rowIndex).getCell(5).getNumericCellValue()));
                threeTarget.setSortCode((i + 1) * 10);
                targetList.add(threeTarget);
            }
            iEvaluateTargetService.saveBatch(targetList);
            //备选项
            for (EvaluateTarget target : targetList) {
                int index = targetList.indexOf(target);
                for (int j = 0; j < 4; j++) {
                    EvaluateTargetOption option = new EvaluateTargetOption();
                    option.setEvaluateTargetId(target.getId());
                    rowIndex = 45 + 4 * index + j;
                    row = sheet.getRow(rowIndex);
                    option.setContent(row.getCell(8).getRichStringCellValue().getString().trim());
                    option.setScore(new BigDecimal(row.getCell(9).getNumericCellValue()));
                    option.setSortCode((j + 1) * 10);
                    optionList.add(option);
                }
            }
            targetList = new ArrayList<>();
            for (int i = 0; i < 6; i++) {
                rowIndex = 57 + 4 * i;
                secondTarget = new EvaluateTarget();
                secondTarget.setEvaluateTypeId(typeId);
                secondTarget.setParentId(firstTarget.getId());
                secondTarget.setName(sheet.getRow(rowIndex).getCell(2).getRichStringCellValue().getString().trim());
                secondTarget.setWeight(new BigDecimal(sheet.getRow(rowIndex).getCell(3).getNumericCellValue()));
                secondTarget.setSortCode((i + 1) * 10);
                targetList.add(secondTarget);
            }
            iEvaluateTargetService.saveBatch(targetList);
            //备选项
            for (EvaluateTarget target : targetList) {
                int index = targetList.indexOf(target);
                for (int j = 0; j < 4; j++) {
                    EvaluateTargetOption option = new EvaluateTargetOption();
                    option.setEvaluateTargetId(target.getId());
                    rowIndex = 57 + 4 * index + j;
                    row = sheet.getRow(rowIndex);
                    option.setContent(row.getCell(8).getRichStringCellValue().getString().trim());
                    option.setScore(new BigDecimal(row.getCell(9).getNumericCellValue()));
                    option.setSortCode((j + 1) * 10);
                    optionList.add(option);
                }
            }
            iEvaluateTargetOptionService.saveBatch(optionList);
            return ResultJson.success("导入成功");
        } catch (Exception ex) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

}

